package com.wedistudio.slidesprovider;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.wedistudio.android.framework.fetcher.URLBuilder;
import com.wedistudio.android.framework.html.HTMLTools;
import com.wedistudio.android.framework.json.JSonObject;
import com.wedistudio.android.framework.service.ServiceResponse;

public class Slide extends JSonObject<Slide> implements ServiceResponse {

	SlideID id;
	Author author;
	Date publicationDate;
	String title;
	String summary;
	URL thumbnail;
	List<SlideID> related;
	Integer nbSlides;
	Integer slideFormat;
	List<URL> slides;
	SlideTags tags;

	public static Slide create(SlideID id) {
		return new Slide(id);
	}

	private Slide(SlideID id) {
		this.id = id;
	}

	public SlideID getId() {
		return id;
	}

	public void setId(SlideID id) {
		this.id = id;
	}

	public Author getAuthor() {
		return author;
	}

	public void setAuthor(Author author) {
		this.author = author;
	}

	public Date getPublicationDate() {
		return publicationDate;
	}

	public void setPublicationDate(Date publicationDate) {
		this.publicationDate = publicationDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public URL getThumbnail() {
		return thumbnail;
	}

	public void setThumbnail(URL thumbnail) {
		this.thumbnail = thumbnail;
	}

	public void addRelated(String source, String id) {
		if (related == null)
			related = new ArrayList<SlideID>();

		related.add(SlideID.create(source, id));
	}

	public List<SlideID> getRelated() {
		return related;
	}

	public Integer getNbSlides() {
		return nbSlides;
	}

	public void setNbSlides(Integer nbSlides) {
		this.nbSlides = nbSlides;
	}

	public Integer getSlideFormat() {
		return slideFormat;
	}

	public void setSlideFormat(Integer slideFormat) {
		this.slideFormat = slideFormat;
	}

	private static URL generateSlideURL(String path, Integer format, int nb) {
		return URLBuilder.create("http://image.slidesharecdn.com/" + path
				+ "/95/slide-" + nb + "-" + format + ".jpg");
	}

	public void populateSlides() {
		if (slides != null)
			return;

		String splittedUrl[] = thumbnail.toString().split("/");

		String path = splittedUrl[4].substring(0,
				splittedUrl[4].indexOf("-thumbnail"));
		List<Integer> formats = new ArrayList<Integer>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -4854198751491564189L;

			{
				add(638);
				add(728);
				add(1024);
			}
		};

		for (Integer format : formats) {
			if (HTMLTools.exists(generateSlideURL(path, format, 1))) {
				slideFormat = format;
				break;
			}
		}

		// not found
		if (slideFormat == null)
			throw new RuntimeException("Cannot find picture for " + id);

		slides = new ArrayList<URL>();

		for (int i = 1; i < nbSlides + 1; i++)
			slides.add(generateSlideURL(path, slideFormat, i));
	}

	public List<URL> getSlides() {
		populateSlides();
		return slides;
	}

	public void addTag(SlideTag tag) {
		if (tags == null)
			tags = new SlideTags();

		tags.add(tag);
	}

	public void removeTag(SlideTag tag) {
		if (tags == null)
			tags = new SlideTags();

		tags.remove(tag);
	}

	public SlideTags getTags() {
		return tags;
	}

	public void setRelated(List<SlideID> related) {
		this.related = related;
	}

	public void setSlides(List<URL> slides) {
		this.slides = slides;
	}

	public void setTags(SlideTags tags) {
		this.tags = tags;
	}
}
